<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('合同产品表列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>合同编码：</label>
                                <input type="text" name="contractCode"/>
                            </li>
                            <li hidden="hidden">
                                <label>产品ID：</label>
                                <input type="text" name="productId"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="contract:contractProduct:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="edit()" shiro:hasPermission="contract:contractProduct:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled hidden" onclick="$.operate.removeAll()" shiro:hasPermission="contract:contractProduct:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning hidden" onclick="$.table.exportExcel()" shiro:hasPermission="contract:contractProduct:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('contract:contractProduct:edit')}]];
        var removeFlag = [[${@permission.hasPermi('contract:contractProduct:remove')}]];
        var prefix = ctx + "contract/contractProduct";

        let contractType=[[${contractType}]];

        //是否是采购合同
        function isContractBuy(){
            if(contractType==contractTypeObj.buy){
                return true;
            }
            return false;
        }
        if(contractType==contractTypeObj.buy){
            $(".btn-primary").text("入库");
        }else if(contractType==contractTypeObj.sell){
            $(".btn-primary").text("出库");
        }

        $(function() {
            var options = {
                url: prefix + "/list?contractType="+contractType,
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                sortName: "contractProductId",
                sortOrder:"desc",
                modalName: "合同产品表",
                showSearch: false,
                showRefresh: false,
                showColumns: false,
                showToggle: false,
                columns: [{
                    checkbox: true,
                },
                    {
                        field: 'contractProductId',
                        title: 'ID',
                        visible: false
                    },
                    {
                        field: 'productId',
                        title: '产品ID',
                        visible: false
                    },
                    {
                        field: 'contractId',
                        title: '合同ID',
                        visible: false,
                    },
                    {
                        field: 'contractType',
                        title: '合同类型',
                        visible: false,
                    },

                    {
                        field: 'contractCode',
                        title: '合同编码',
                    },
                    {
                        field: 'name',
                        title: '产品名称',
                    },
                    {
                        field: 'code',
                        title: '产品编码',
                    },
                    {
                        field: 'img',
                        title: '产品图片',
                        visible: false,
                        formatter: function(value, row, index) {
                            return $.table.imageView(value);
                        }
                    },
                    {
                        field: 'price',
                        title: '产品价格'
                    },
                    {
                        field: 'num',
                        title: '合同数量'
                    },
                    {
                        field: 'useNum',
                        title: isContractBuy()?'已入库数量':'已出库数量',
                    },
                    {
                        field: 'remainNum',
                        title: isContractBuy()?'待入库数量':'待出库数量',
                    },
                    {
                        title: '操作',
                        align: 'center',
                        formatter: function(value, row, index) {
                            var actions = [];
                            if(row.remainNum==0){
                                let title=isContractBuy()?"已完成入库":"已完成出库";
                                actions.push('<a class="btn btn-xs " href="javascript:void(0)" onclick="openStoreDetail(\'' + row.contractProductId + '\')" ><i class="fa fa-search">'+title+'</i></a> ');

                            }else if(row.remainNum>0){
                                let title=isContractBuy()?"入库详情":"出库详情";
                                actions.push('<a class="btn btn-dark btn-xs " href="javascript:void(0)" onclick="openStoreDetail(\'' + row.contractProductId + '\')"><i class="fa fa-search">'+title+'</i></a> ');

                            }
                            return actions.join('');
                        }
                    }
                    ]
            };
            $.table.init(options);
        });

        //出|入库
        function edit(){
            let data = $("#bootstrap-table").bootstrapTable('getSelections');
            if(data){
                let title= data[0].contractType==contractTypeObj.buy?"入库":"出库";
                let contractProductId=data[0].contractProductId;
                openWinRefresh(title,prefix + "/edit/"+contractProductId);
            }
        }

        //入库详情
        function openStoreDetail(orderDetailId){
            if(isContractBuy()){
                openWin("查看入库详情",ctx+"warehouse/mallStoreDetail?orderDetailId="+orderDetailId)
            }else{
                openWin("查看出库详情",ctx+"warehouse/sellStoreDetail?orderDetailId="+orderDetailId)
            }

        }
    </script>
</body>
</html>
